//
//  GSCommonLoginPhoneTextField.m
//  GSLoginDemo
//
//  Created by guoshikeji_a on 2018/7/27.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "GSCommonLoginPhoneTextField.h"

#define GSCommonLoginPhoneTextFieldPadding @" "

@implementation GSCommonLoginPhoneTextField

- (instancetype)init
{
    self = [super init];
    if (self) {
//        self.keyboardType = UIKeyboardTypeNamePhonePad;
        self.delegate = self;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldsDidChangedTextNotification:) name:UITextFieldTextDidChangeNotification object:self];
    }
    return self;
}


#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//    if ([@"1234567890" rangeOfString:string].location!=NSNotFound) {
//        NSUInteger length = self.phoneNumber.length;
//        if (self.phoneInvalidBlock) {
//            self.phoneInvalidBlock(length>=10);
//        }
//        if (length>=11) {
//            return false;
//        }
//        if (range.location==3||range.location==8) {
//            self.text = [self.text stringByAppendingString:GSCommonLoginPhoneTextFieldPadding];
//        }
//        return true;
//    }
//    if ([string isEqualToString:@""]) {
//        if (self.phoneInvalidBlock) {
//            self.phoneInvalidBlock(false);
//        }
//        return true;
//    }
//    return false;
    return true;
}




- (NSString *)phoneNumber {
    return self.text;
}

#pragma mark -noti
- (void)textFieldsDidChangedTextNotification:(NSNotification *)noti {
    BOOL valid = self.phoneNumber.length;
    if (self.phoneInvalidBlock) {
        self.phoneInvalidBlock(valid);
    }
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
